﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace ArbolandiaBL
{
    internal static class CompiledQuerys
    {
        internal static readonly Func<ArbolandiaDBDataContext, Guid, string> GetImageUrl = CompiledQuery.Compile((ArbolandiaDBDataContext db, Guid pictureGuidId)
           => db.fnGetImageUrl(pictureGuidId));

        internal static readonly Func<ArbolandiaDBDataContext, Int32, decimal?> GetLowerPriceSubCategory = CompiledQuery.Compile((ArbolandiaDBDataContext db, Int32 id)
           => db.fnGetLowerPriceSubCategory(id));

        internal static readonly Func<ArbolandiaDBDataContext, Int32,Int32, string> GetProductCode = CompiledQuery.Compile((ArbolandiaDBDataContext db, Int32 idprod,Int32 iddetail)
           => db.Product_ProductDetail.Where(w=>w.Id == iddetail && w.Product_ProductId == idprod).FirstOrDefault<Product_ProductDetail>().CodigoProducto);

        internal static readonly Func<ArbolandiaDBDataContext, Int32, int?> GetTotalProductsSubCategory = CompiledQuery.Compile((ArbolandiaDBDataContext db, Int32 id)
           => db.fnGetTotalProductsSubCategory(id));

        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<UISubcategoryResume>> GetSubCategory = (ArbolandiaDBDataContext db, Int32 id)
         => (from p in db.spGetSubCategory(id)
             select new UISubcategoryResume()
             {
                 Id = p.Id,
                 IdCategory = p.IdCategory,
                 ImageUrl = p.ImageUrl,
                 LowerPrice = p.LowerPrice,
                 Name = p.Name,
                 TotalProducts = p.TotalProducts
             }
                ).ToList();



        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<Int32>> GetSubCategorysByCategory = CompiledQuery.Compile((ArbolandiaDBDataContext db, Int32 id)
          => (from p in db.Product_SubCategory.Where(w => w.Product_CategoryId == id)
              select p.Id).ToList()
              );

        internal static readonly Func<ArbolandiaDBDataContext, List<UIProductsHome>> GetOfertasDestacadosHome = (ArbolandiaDBDataContext db)
         => (from p in db.spGetOfertasDestacadosHome()
             select new UIProductsHome()
                        {
                            Id = p.Id,
                            ImageUrl = p.ImageUrl,
                            IsDestacado = p.IsDestacado,
                            IsOferta = p.IsOferta,
                            LowerPrice = p.LowerPrice,
                            Name = p.Name,
                            DiscountDescription = p.DiscountDescription
                        }
             ).ToList();


        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<UIProductsHome>> GetOfertasByCategoria = (ArbolandiaDBDataContext db, Int32 id)
         => (from p in db.spGetOfertasByCategoria(id)
             select new UIProductsHome()
             {
                 Id = p.Id,
                 ImageUrl = p.ImageUrl,
                 IsDestacado = p.IsDestacado,
                 IsOferta = p.IsOferta,
                 LowerPrice = p.LowerPrice,
                 Name = p.Name
             }
             ).ToList();


        internal static readonly Func<ArbolandiaDBDataContext, Int32, UISubcategoryDetail> GetSubCategoryDetail = (ArbolandiaDBDataContext db, Int32 id)
         => (from p in db.spGetSubCategoryDetail(id)
             select new UISubcategoryDetail()
             {
                 Id = p.Id,
                 Name = p.Name,
                 MetaDescription = p.FullDescription,
                 UrlVideo = p.VideoUrl,
                 IdCategory = p.IdCategory,
                 CategoryName = p.NameCategory,
                 ImageUrl = p.PictureUrl
             }).FirstOrDefault();


        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<UIProductResume>> GetProductsBySubCategory = (ArbolandiaDBDataContext db, Int32 id)
         => (from p in db.spGetProductsBySubCategory(id)
             select new UIProductResume()
             {
                 Id = p.Id,
                 Name = p.Name,
                 ShortDescription = p.ShortDescription,
                 LowerPrice = p.LowerPrice,
                 Size = p.Size,
                 ImageUrl = p.PictureUrl
             }
             ).ToList();


        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<UIImageProduct>> GetImagesProductThumbnails = (ArbolandiaDBDataContext db, Int32 id)
         => (from p in db.spGetImagesProduct(id)
             where p.isThumbnail == true
             select new UIImageProduct()
             {
                 GuidId = p.guidId.ToString(),
                 ImageUrl = p.imageUrl,
                 iOrder = p.iorder,
                 LinkUrl = p.LinkUrl,
                 ToolTip = p.ToolTip
             }
             ).ToList();


        internal static readonly Func<ArbolandiaDBDataContext, List<ADProductSimple>> GetAllProducts = (ArbolandiaDBDataContext db)
        => (from p in db.spGetAllProducts()
            select new ADProductSimple()
            {
                Id = p.Id,
                Name = p.Name,
                ShortDescription = p.ShortDescription,
                LowerPrice = p.LowerPrice,
                Size = p.Size,
                MesesFloracion = p.MesesFloracion,
                MesesFrutos = p.MesesFrutos,
                PictureUrl = p.PictureUrl
            }
            ).ToList();

        internal static readonly Func<ArbolandiaDBDataContext, Int32?, Int32> GetProductId = (ArbolandiaDBDataContext db, Int32? id)
        => (from p in db.spGetProductId(ref id)
            select p.productid).FirstOrDefault<Int32>();

        internal static readonly Func<ArbolandiaDBDataContext, Int32, Int32?> GetSubCategoryByProductId = CompiledQuery.Compile((ArbolandiaDBDataContext db, Int32 id)
           => db.fnGetSubCategoryByProductId(id));

        internal static readonly Func<ArbolandiaDBDataContext, Int32, List<ContentManagement>> GetContentManagements = (ArbolandiaDBDataContext db, Int32 id)
        => (from p in db.spGetContentManagement(id)
            select new ContentManagement()
            {
                Id = p.Id,
                Category = p.Category,
                Content = p.Content,
                UrlImage = p.UrlImage
            }
            ).ToList();


    }
}
